package com.me.app;


import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import com.me.data.DataService;
import com.me.fragment.ProgressBarFragment;
import com.me.fragment.ViewPagerFragment;
import com.me.util.AppConstant;
import com.me.util.AppUtil;
import com.me.util.GenNotification;

public class HomeActivity extends FragmentActivity {
	
	boolean doubleBackPressedOnce;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);

		Toast.makeText(getBaseContext(), AppUtil.App.currCalendarName, Toast.LENGTH_SHORT).show();
		AppUtil.App.dataService = new DataService(getContentResolver(), getBaseContext());

		// Setting ActionBar
		final ActionBar actionBar = getActionBar();
		
		actionBar.setDisplayShowTitleEnabled(false);							// remove activity title
		SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this,	// set spinner navigation
				R.array.event_type,
				android.R.layout.simple_spinner_dropdown_item);
		
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		actionBar.setListNavigationCallbacks(spinnerAdapter, getSpinnerListener());
	
		if (AppUtil.App.defaultEventType.equals(AppConstant.EVENT_PENDING))		// set event type on start
			actionBar.setSelectedNavigationItem(AppConstant.EVENT_INDEX_PENDING);		
		else if (AppUtil.App.defaultEventType.equals(AppConstant.EVENT_CALENDAR))
			actionBar.setSelectedNavigationItem(AppConstant.EVENT_INDEX_CALENDAR);
		else if (AppUtil.App.defaultEventType.equals(AppConstant.EVENT_TITLE))
			actionBar.setSelectedNavigationItem(AppConstant.EVENT_INDEX_TITLE);
		else if (AppUtil.App.defaultEventType.equals(AppConstant.EVENT_LOCATION))
			actionBar.setSelectedNavigationItem(AppConstant.EVENT_INDEX_LOCATION);
			
		GenNotification.show(getPackageName(), this);

	}

// ***************************************************************************************************************
// Action Bar
	private OnNavigationListener getSpinnerListener() {
		return new OnNavigationListener() {
			@Override
			public boolean onNavigationItemSelected(int position, long itemId) {

				AppUtil.Event.eventType = position;
				new updateEventData().execute();	
				return true;
			}
		};
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.home, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;

		// Handle presses on the action bar items
		switch (item.getItemId()) {
		case R.id.action_go_to_today:
			ViewPagerFragment.getViewPager().setCurrentItem(AppConstant.VIEW_PAGER_FIRST_PAGE);
			//ViewPagerFragment.getViewPager().getAdapter().notifyDataSetChanged();
			return true;
			
		case R.id.action_new:
			i = new Intent(this, EventCreateActivity.class);
			i.putExtra("IsEdit", false);
			i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			return true;
			
		case R.id.action_settings:
			i = new Intent(this, SettingsActivity.class);
			startActivity(i);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

// Action Bar
// ***************************************************************************************************************

	
	public class updateEventData extends AsyncTask<String, String, String> {

		private Fragment fragment;
		private Fragment progressBarFragment = ProgressBarFragment.newInstance();
		private FragmentTransaction fragmentTransaction;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			AppUtil.Event.clear();
		}

		@Override
		protected String doInBackground(String... params) {
		
//			fragmentTransaction = getSupportFragmentManager()
//					.beginTransaction();	
//			fragmentTransaction.replace(R.id.frame, progressBarFragment);
//			fragmentTransaction.commit();
		
			fragment = ViewPagerFragment.newInstance();
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

			fragmentTransaction = getSupportFragmentManager()
					.beginTransaction();
			fragmentTransaction.replace(R.id.frame, fragment);
			fragmentTransaction.commit();
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		GenNotification.show(getPackageName(), this);
		Toast toast = Toast.makeText(getBaseContext(), AppConstant.TOAST_SHOW_NOTIFICATION, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
		toast.show();
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (AppUtil.Event.updateRequired()) {
			new updateEventData().execute();
		}
	}

	
	@Override
	public void onBackPressed() {

	    if (doubleBackPressedOnce) {
	        super.onBackPressed();
	        return;
	    }
	    this.doubleBackPressedOnce = true;
		Toast.makeText(getBaseContext(), AppConstant.TOAST_BACK_PRESSED, Toast.LENGTH_SHORT).show();
	    new Handler().postDelayed(new Runnable() {

	        @Override
	        public void run() {
	        	doubleBackPressedOnce = false;                       
	        }
	    }, 2000);			
	}

}
